#include "stack.h"

//stack init
void STInit(ST* pst)
{
	assert(pst);
	pst->data = NULL;
	pst->capacity = 0;
	pst->top = 0;
}

//stack destroy
void STDestroy(ST* pst)
{
	assert(pst);
	free(pst->data);
	pst->data = NULL;
	pst->top = 0;
	pst->capacity = 0;
}

//stack push
// -1==NG  1==OK     
int STPush(ST* pst, STDataType value)
{
	assert(pst);
	if (pst->top >= pst->capacity)
	{
		//realloc capacipt
		int newcapacity = (pst->capacity == 0) ? 4 : (2 * pst->capacity);
		STDataType* newdatapointer = realloc(pst->data, (newcapacity * sizeof(STDataType)));
		if (!newdatapointer)
		{
			perror("STPush::realloc::");
			return -1;
		}
		pst->data = newdatapointer;
		pst->capacity = newcapacity;
	}
	//push value
	pst->data[pst->top] = value;
	pst->top++;
	return 1;
}

//stack pop
void STPop(ST* pst)
{
	assert(pst);
	assert(pst->top > 0);
	pst->top--;
}

//return stack top
//if top data == NULL, assert error
STDataType STTop(ST* pst)
{
	assert(pst && pst->top > 0);
	return pst->data[pst->top - 1];
}

//stack is empty
//if stack==empty, return true
//else return false
bool STEmpty(ST* pst)
{
	assert(pst);
	return pst->top == 0;
}

//stack size
//data quantity
int STSize(ST* pst)
{
	assert(pst);
	return pst->top;
}

